package com.example.his.api.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.exception.HisException;
import com.example.his.api.mis.controller.form.role.*;
import com.example.his.api.mis.service.RoleService;
import kotlin.jvm.Throws;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@RestController("MisRoleController")
@RequestMapping("/mis/role")
public class RoleController {
  @Resource
  private RoleService roleService;
  
  @GetMapping("/searchAllRole")
  public R searchAllRole() {
    ArrayList <HashMap> list = roleService.searchAllRole();
    
    return R.ok().put("list", list);
  }
  
  // 根据id查询角色
  @PostMapping("/searchById")
  @SaCheckPermission(value = { "ROOT", "ROLE:SELECT" }, mode = SaMode.OR)
  public R searchById(@RequestBody @Valid SearchRoleByIdForm form) {
    HashMap map = roleService.searchById(form.getId());
    return R.ok().put("result", map);
  }
  
  @PostMapping("/updateById")
  @SaCheckPermission(value = { "ROOT", "ROLE:UPDATE" }, mode = SaMode.OR)
  public R updateById(@RequestBody @Valid UpdateRoleByIdForm form) {
    ArrayList <Number> permissions = form.getPermissions();
    ArrayList <Number> defaultPermissions = form.getDefaultPermissions();
    
    Map param = BeanUtil.beanToMap(form);
    if(permissions != null) {
      param.replace("permissions", permissions.toString());
    }
    if(defaultPermissions != null) {
      param.replace("defaultPermissions", defaultPermissions.toString());
    }
    boolean b = roleService.updateById(param);
    return R.ok().put("result", b);
  }
  
  @PostMapping("/insert")
  @SaCheckPermission(value = { "ROOT", "ROLE:INSERT" }, mode = SaMode.OR)
  public R insert(@RequestBody @Valid InsertRoleForm form) {
    Integer roleId = roleService.searchByRoleName(form.getRoleName());
    if(roleId != null) {
      throw new HisException("该角色已存在");
    }
    ArrayList <Number> permissions = form.getPermissions();
    String string = permissions.toString();
    Map param = BeanUtil.beanToMap(form);
    param.replace("permissions", string);
    boolean bool = roleService.insert(param);
    return R.ok().put("result", bool);
  }
  
  @PostMapping("/deleteByIds")
  @SaCheckPermission(value = { "ROOT", "ROLE:DELETE" }, mode = SaMode.OR)
  public R deleteByIds(@RequestBody @Valid DeleteRoleByIdsForm form) {
    ArrayList <Integer> ids = form.getIds();
    int i = roleService.deleteByIds(ids);
    return R.ok().put("result", i);
  }
  
  @PostMapping("/searchByPage")
  @SaCheckPermission(value = { "ROOT", "ROLE:SELECT" }, mode = SaMode.OR)
  public R searchByPage(@RequestBody SearchRoleByPageForm form) {
    Integer page = form.getPage();
    Integer length = form.getLength();
    int start = (page - 1) * length;
    Map param = BeanUtil.beanToMap(form);
    param.put("start", start);
    
    PageUtils pageUtils = roleService.searchByPage(param);
    return R.ok().put("result", pageUtils);
  }
  
}
